#include "myhead.h"
int main()
{
    //创建一个套接字
    int csock= socket(AF_INET,SOCK_STREAM,0);
    
    int opt=1;
    setsockopt(csock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

    struct sockaddr_in myaddr;
    myaddr.sin_family=AF_INET;
    myaddr.sin_addr.s_addr=INADDR_ANY;
    myaddr.sin_port=htons(5000);
    bind(csock,(struct sockaddr *)&myaddr,sizeof(myaddr));

    struct sockaddr_in youaddr;
    youaddr.sin_family=AF_INET;
    inet_pton(AF_INET,"192.168.72.52",&youaddr.sin_addr);
    youaddr.sin_port=htons(10000);
    connect(csock,(struct sockaddr *)&youaddr,sizeof(youaddr));
    printf("连接成功\n");

    fd_set myset;
    struct timeval mytime;
    bzero(&mytime,sizeof(mytime));//要bzero要不有问题
    mytime.tv_sec=5;
    FD_ZERO(&myset);
    char buf[10]="";
    while(1)
    {
        FD_SET(0,&myset);
        FD_SET(csock,&myset);
        bzero(buf,sizeof(buf));
        int ret = select(csock+1,&myset,NULL,NULL,&mytime);//返回值：大于0成功，小于0失败，等于0超时
        if(ret <0)
        {
            perror("检测失败\n");
        }
        if(ret ==0)
        {
            perror("超时\n");
        }
        
        if(FD_ISSET(csock,&myset))
        {
            recv(csock,buf,sizeof(csock),0);
            printf("服务器发来%s\n",buf);
        }
        if(FD_ISSET(0,&myset))
        {
            scanf("%s",buf);
            send(csock,buf,strlen(buf),0);
        }
    }
    
}